*********************************************************
* This program computes ivreg regressions with two simultaneous cluster variables
* The idea follows Cameron, Gelbach, and Miller (2006)
* Note that I also allow for lots of _I fixed effects type variables that you don't care about
* I drop them from the error matrix so we don't get lots of unpleasant matrix not semi definite errors
*
* Ben Olken
* September 16, 2006
*********************************************************
cap program drop bencgmivreg
program define bencgmivreg, eclass
syntax varlist(fv) [if], endog(varlist) instr(varlist) cluster1(varname) cluster2(varname) [numrealvars(int 0)] 

********************************
* Syntax
* bencgmivreg varlist
********************************

tokenize "`varlist'"
local theDepVar = "`1'"
macro shift 1
local theXVar = "`*'"

quietly {
	ivregress 2sls `theDepVar' (`endog' = `instr') `theXVar' `if', cluster(`cluster1')
	matrix v1 = e(V)
	ivregress 2sls `theDepVar' (`endog' = `instr') `theXVar' `if', cluster(`cluster2')
	matrix v2 = e(V)
	
	tempvar temp1
	egen `temp1' = group(`cluster1' `cluster2')
	ivregress 2sls `theDepVar' (`endog' = `instr') `theXVar' `if', cluster(`temp1')
	matrix v3 = e(V)
	
	matrix b = e(b)
	local obs = e(N)
	tempvar sample 
	g `sample' = e(sample)
	
	matrix v4 = v1 + v2 - v3
	
	if `numrealvars' == 0 {
		matrix vnew = v4
		matrix bnew = b
	}
	else {
		matrix vnew = v4[1..`numrealvars',1..`numrealvars']
		matrix bnew = b[1,1..`numrealvars']
	}
	
	ereturn post bnew vnew, obs(`obs') esample(`sample')
}

ereturn disp
 
end

